function plotmultiple_irfs(irfmat, in)
% PLOTMULTIPLE_IRFS 
% Inputs  
% IRFMAT    [nser nx nper+1 nmode] 
% IN. 
% .NC                Number of columns (default is 3) 
%                    Number of rows then chosen to fit all graphs 
% .FLAG_IRFBYSHO     ==1 group by shocks 
% .VARNAMES          Names of variables to plot 
% .MODENAMES         Names of modes 
% .SHONAMES          Names of Shocks 
% .SAVENAME 
% .FIG_NUM           Vector of figures that will add to the title;
%                    If number if non-zero will store a matlab fig file
%                    with: IRF fig <number> 
% 
% Notes.
% 1) If only 1 mode supplied, supress legend
% 2) Do not allow to add IRFS here for levels, this must have been done
% outside code 
% 3) 

% =========================================================================
% 1. Check size and unpack parameters 
% =========================================================================
cucd=pwd; 

[nz, nsh, nper, nmode] = size(irfmat);
[junk, in] = ch_field(in, 'flag_irfbysho', 1);
[junk, in] = ch_field(in, 'nc', 3);

[junk, in] = ch_field(in, 'legfont', 11);
[junk, in] = ch_field(in, 'titfont', 12);
[junk, in] = ch_field(in, 'suptitfont', 14);
[junk, in] = ch_field(in, 'flag_landscape', 0);
[junk, in] = ch_field(in, 'flag_nosubtitulo', 0);
[junk, in] = ch_field(in, 'axesfontsize', 9);
[junk, in] = ch_field(in, 'add_hline', 0);

flag_irfbysho = in.flag_irfbysho;
varnames = in.varnames;
modenames = in.modenames;
shonames = in.shonames;
savepath = in.savepath;
savename = in.savename; 
if length(modenames) ~= nmode;
    error('nmode does not match the length of modenames');
end
if length(varnames) ~=nz; 
    error('Names of variables does not match IRF matrix'); 
end 



if flag_irfbysho==0
    [junk, in] = ch_field(in, 'fig_indic' , zeros(nz,1) ); 
    if length(in.fig_indic)~=nz; 
        error('IRFS per series, need FIG_INDIC match dimension') 
    end 
    disp('IRFS organized by series'); 
    pagenames=varnames; 
else 
    [junk, in] = ch_field(in, 'fig_indic' , zeros(nsh,1) ); 
    if length(in.fig_indic)~=nsh; 
        error('IRFS per series, need FIG_INDIC match dimension') 
    end 
    disp('IRFS organized by shocks'); 
    pagenames=shonames; 
end 
if any(in.fig_indic~=0) 
    disp('Print figs for:'); 
    printcell([pagenames(:) num2cprec(in.fig_indic(:))]); 
end 
    

% =========================================================================
% 2. Size of plots 
% =========================================================================
nc = in.nc;
if flag_irfbysho == 1;
    nr = ceil(nz/nc);
    ss.rnames = varnames;
    ss.cnames = {''};
    gvec = zeros(nsh, 1);
else
    nr = ceil(nsh/nc);
    ss.rnames={''};
    ss.cnames=shonames;
    gvec=zeros(nz,1);
end

ss.add_hline = in.add_hline;
ss.subdesign(1) = nr;
ss.subdesign(2) = nc;
ss.bnames = modenames;
ss.legfont=in.legfont;
ss.suptitfont=in.suptitfont;
ss.titfont=in.titfont;
ss.axesfontsize=in.axesfontsize; 


if in.flag_landscape==0
    paper_orientation = 'Portrait';
    paper_position=[0.75 0.5 7 9.5];
else
    paper_orientation = 'Landscape';
    paper_position    =[0.1 0.1 10.5 7.5];
end

ss.style    ={'k-','b--',  'r:','c.-','k^:','gh-'};
ss.width    =[2     2         2    2       2      2];
ss.markwidth=[1     3           3    4       7      7];

ss.flag_noxtick=0;
ss.flag_nolegend=1;
ss.suptitfont  =in.suptitfont; 

if flag_irfbysho == 1;
    for ii = 1:nsh
        if in.fig_indic(ii)~=0
            ss.suptitle=['Figure ',num2str(in.fig_indic(ii)),': IRF to ',shonames{ii}];
        else
            ss.suptitle = ['IRF: ', shonames{ii}];
        end
        ghand = plot_multiple3d(irfmat(:, ii, :, :), ss);
        if in.flag_nosubtitulo==0
        subtitulo(savepath);
        end 
        set(ghand,'PaperOrientation', paper_orientation);
        set(ghand,'PaperPosition',paper_position);
        gvec(ii) = ghand;
    end
else
    for ii = 1:nz
        ss.suptitle = ['IRF: ', varnames{ii}];
        ghand =plot_multiple3d(irfmat(ii,:,:,:),ss);
        subtitulo(savepath);
        set(ghand,'PaperOrientation', paper_orientation);
        set(ghand,'PaperPosition',paper_position);
        gvec(ii) = ghand;
    end
end

posfigs=find( in.fig_indic~= 0 );
if ~isempty(posfigs);
    cd(savepath);
    for ii=1:length(posfigs);
        saveas( gvec(posfigs(ii)),...
            ['Figure ',num2str(in.fig_indic(posfigs(ii))),' IRF to ',shonames{posfigs(ii)}],...
            'fig');
    end
    cd(cucd);
end

save_multipages(gvec, savename, savepath);

close all;

end % of main function

    